package generator;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BaiZeGenerator {
    public static void main(String[] args) {
        List<String> tables = Arrays.asList(
                "Categories",
                "Comments",
                "Likes",
                "Notes",
                "PostCategories",
                "PostContent",
                "Posts",
                "PostTags",
                "Resources",
                "Tags",
                "Users"
        );
        String path = System.getProperty("user.dir") + "/code-ganerate";
        System.out.println(path);
        FastAutoGenerator.create(
                "jdbc:mysql://14.103.49.89:8306/minespace?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false",
                "root",
                "123456"
        ).globalConfig(
                builder -> {
                    builder.author("baize")
                            .fileOverride()
                            .enableSwagger()
                            .dateType(DateType.TIME_PACK)
                            .outputDir(path + "\\src\\main\\java\\code")
                            .commentDate("yyyy-MM-dd");
                }
        ).packageConfig(
                builder -> {
                    builder.parent("com.baize.minespace")
                            .entity("pojo")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .pathInfo(Collections.singletonMap(
                                    OutputFile.mapperXml,
                                    path + "\\src\\main\\java\\code\\com\\baize\\minespace\\mapper\\xml"
                            ))
//                            .other("other") // 设置dto包名
//                            .pathInfo(Collections.singletonMap(OutputFile.other, path + "/src/main/java//code/other"))
                            .build();

                }
        ).strategyConfig(
                builder -> {
                    builder.addInclude(tables)// 设置需要生成的表名
                            .enableSkipView()// 跳过视图
                            .entityBuilder()// 实体类
                            .enableTableFieldAnnotation()// 开启 Boolean 类型字段移除 is 前缀
                            .enableLombok()// 开启 lombok 模型	默认值:false
                            .enableTableFieldAnnotation()// 开启生成实体时生成字段注解	默认值:false
                            .enableRemoveIsPrefix()// 开启 Boolean 类型字段移除 is 前缀
//                            .enableFileOverride()// 开启覆盖已生成文件
                            .mapperBuilder()// Mapper 策略配置
                            .superClass(BaseMapper.class)// 设置父类
                            .enableBaseResultMap()// 开启 BaseResultMap
                            .formatMapperFileName("%sMapper")// Mapper XML 文件名格式
                            .serviceBuilder()// Service 策略配置
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .controllerBuilder()
                            .enableRestStyle()
                            .enableHyphenStyle()
                            .formatFileName("%sController")
                            .build();
                }
        )
//                .injectionConfig(
//                builder -> {
//                    builder.beforeOutputFile(
//                            (tableInfo, objectMap) -> {
//                                System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
//                            }
//                    ).customFile(
//                            Collections.singletonMap("%sDto.java", "/templates/Dto.java.vm")
//                    ).customFile(
//                            Collections.singletonMap("%sVo.java", "/templates/Vo.java.vm")
//                    );
//                }
//        )
                .templateEngine(new VelocityTemplateEngine()).execute();
    }
}
